package com.start.shop.api.param.coupon;

import java.util.Calendar;
import java.util.Date;

import com.start.shop.common.constant.TableFeildCode;
import com.start.shop.modules.repository.entity.Coupon;
import com.start.shop.modules.repository.entity.CouponConfig;
import com.start.shop.modules.repository.entity.User;

import lombok.Data;

@Data
public class IssueCouponParam {
	int couponSortId;
	int num;
	User user;

	public  static Coupon IssueCouponParamToCoupon(IssueCouponParam issueCouponParam, CouponConfig couponConfig) {
		Coupon coupon = new Coupon();
		coupon.setUserId(issueCouponParam.getUser().getUserId());
		coupon.setSortId(issueCouponParam.getCouponSortId());
		coupon.setCouponStatus(TableFeildCode.T_COUPON_STATUS_NOTUSE.getCode());
		coupon.setStartDate(couponConfig.getStartDate());
		if (couponConfig.getEndDate()== null) {
			 Calendar startDate=Calendar.getInstance();
			 startDate.setTime(couponConfig.getStartDate());
			 startDate.add(Calendar.DATE, couponConfig.getCouponTerm());
			coupon.setEndDate(startDate.getTime());
		}
		else {
			coupon.setEndDate(couponConfig.getEndDate());
		}
		coupon.setCreateDate(new Date());

		return coupon;
	}
}
